// Copyright 2008 and onwards Matt Burkhart, Mike Chan.
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.

#include "path_utils.h"

using namespace std;

// See header for documentation.

string Basename(const string& path) {
  string result(path);
  result.erase(0, path.rfind("/") + 1);
  return result;
}

string StripBasename(const string& path) {
  string result(path);
  result.resize(max<int>(0, path.rfind("/")));
  return result;
}

// TODO: This implementation could probably be cleaner.
string Join(const string& path1, const string& path2) {
  // Get path1 without trailing "/"
  string result = path1;
  if (result.size() > 0 && result.rfind("/") == result.size() - 1) {
    result.resize(result.size() - 1);
  }

  // Return the result with only a single "/" between.
  if (path1.size() > 0 && path2.size() > 0 && path2[0] != '/') {
    result += "/";
  }
  result += path2;

  return result;
}
